using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Users : BasePage
{
    public override Acms.Bll.Role.Roles[] AllowedRoles
    {
        get { return new Acms.Bll.Role.Roles[] { Acms.Bll.Role.Roles.Developer, Acms.Bll.Role.Roles.MembershipViewer, Acms.Bll.Role.Roles.MembershipAdministrator }; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //If user has membershipadmin role then allow access to edit functions
            Acms.WebControls.CommandField deleteUserField = null;
            foreach(DataControlField column in gvUsers.Columns)
            {
                if(column is Acms.WebControls.CommandField && ((Acms.WebControls.CommandField)column).CommandName == "Delete")
                {
                    deleteUserField = column as Acms.WebControls.CommandField;
                }
            }

            if (deleteUserField != null)
            {
                if (User.IsInRole("MemberAdmin") || User.IsInRole("Developer"))
                {
                    deleteUserField.Visible = true;
                }
                else
                {
                    deleteUserField.Visible = false;
                }
            }

            txtStartDate.MinDate = DateTime.Now;
            txtEndDate.MinDate = DateTime.Now;

            foreach (DataControlField field in gvUserRoles.Columns)
            {
                if (field is Acms.WebControls.DateTimeField)
                {
                    ((Acms.WebControls.DateTimeField)field).MinValue = DateTime.Now;
                }
            }
            SetMode(Modes.SelectUser);
        }
    }
    protected void dsUsers_SelectData(object sender, Acms.DataControls.SelectEventArgs<Acms.Bll.User> e)
    {

        e.SetData(Acms.Bll.User.Search(AcmsContext.SiteId));
    }
    /*protected void lblAddress_DataBinding(object sender, EventArgs e)
    {
        Label lbl = (Label)sender;
        DataGridItem row = lbl.NamingContainer as DataGridItem;
        Acms.Bll.User user = row.DataItem as Acms.Bll.User;
        lbl.Text = user.GetFullAddress(", ");
    }

    protected void dgUsers_SelectedIndexChanged(object sender, EventArgs e)
    {
        dvUser.PageIndex = dgUsers.SelectedItem.DataSetIndex;
        userPages.DataBind();
    }*/
    protected void Label1_DataBinding(object sender, EventArgs e)
    {
        Label lbl = sender as Label;
        if (lbl != null)
        {
            GridViewRow row = lbl.NamingContainer as GridViewRow;
            if (row != null)
            {
                Acms.Bll.User user = row.DataItem as Acms.Bll.User;
                if (user != null)
                {
                    lbl.Text = user.GetFullAddress(", ");
                }
            }
        }
    }

    protected void gvUsers_SelectedIndexChanged(object sender, EventArgs e)
    {
        gvUsers.Visible = false;
        pnlDetails.Visible = true;

        dvUserDetails.PageIndex = gvUsers.SelectedIndex + (gvUsers.PageIndex * gvUsers.PageSize);
        dvUserDetails.DataBind();

        gvUserRoles.DataBind();

        Acms.Bll.User user = dvUserDetails.DataItem as Acms.Bll.User;
        lblUserName.Text = user.DisplayName;

        mpUser.SelectedIndex = 0;
        tabUser.SelectedIndex = 0;

        SetMode(Modes.UserDetailsView);

    }
    protected void dsUserRoles_SelectData(object sender, Acms.DataControls.SelectEventArgs<Acms.Bll.UserRole> e)
    {
        int userId = (int)dvUserDetails.DataKey["Id"];
        e.SetData(Acms.Bll.UserRole.GetAllActiveByUserIdSiteId(userId, AcmsContext.SiteId));
    }

    public enum Modes
    {
        SelectUser,
        UserDetailsView,
        UserDetailsEdit,
        UserRolesDisplay,
        UserRolesEdit,
        UserRolesAdd
    }

    private Modes currentMode;

    public Modes CurrentMode
    {
        get { return currentMode; }
    }

    public void SetMode(Modes mode)
    {
        if (currentMode != mode)
        {
            currentMode = mode;

            //hide all links

            lnkSelectUser.Visible = false;
            lnkEdit.Visible = false;
            lnkSave.Visible = false;
            lnkCancel.Visible = false;
            lnkAddRole.Visible = false;

            //hode all elements
            gvUsers.Visible = false;
            pnlDetails.Visible = false;

            gvUserRoles.Visible = false;
            wizAddUserRole.Visible = false;

            switch (currentMode)
            {
                case Modes.SelectUser:
                    gvUserRoles.SelectedIndex = -1;
                    gvUsers.Visible = true;
                    break;
                case Modes.UserDetailsView:
                    pnlDetails.Visible = true;
                    
                    mpUser.SelectedIndex = 0;
                    tabUser.SelectedIndex = 0;

                    if (dvUserDetails.CurrentMode != DetailsViewMode.ReadOnly)
                    {
                        dvUserDetails.ChangeMode(DetailsViewMode.ReadOnly);
                    }

                    lnkSelectUser.Visible = true;

                    if (User.IsInRole("Developer") || User.IsInRole("MemberAdmin"))
                        lnkEdit.Visible = true;
                    else
                        lnkEdit.Visible = false;

                    break;
                case Modes.UserDetailsEdit:
                    pnlDetails.Visible = true;

                    mpUser.SelectedIndex = 0;
                    tabUser.SelectedIndex = 0;

                    if (dvUserDetails.CurrentMode != DetailsViewMode.Edit)
                    {
                        dvUserDetails.ChangeMode(DetailsViewMode.Edit);
                    }

                    lnkSave.Visible = true;
                    lnkCancel.Visible = true;

                    break;
                case Modes.UserRolesDisplay:
                    pnlDetails.Visible = true;

                    mpUser.SelectedIndex = 1;
                    tabUser.SelectedIndex = 1;

                    gvUserRoles.SelectedIndex = -1;
                    gvUserRoles.EditIndex = -1;
                    gvUserRoles.Visible = true;
                    
                    lnkSelectUser.Visible = true;
                    lnkAddRole.Visible = true;
                    break;

                case Modes.UserRolesAdd:
                    pnlDetails.Visible = true;

                    mpUser.SelectedIndex = 1;
                    tabUser.SelectedIndex = 1;

                    wizAddUserRole.Visible = true;
                    wizAddUserRole.ActiveStepIndex = 0;

                    lnkCancel.Visible = true;
                    break;
            }

        }
    }

    protected override object SaveViewState()
    {
        ViewState["Mode"] = currentMode;
        return base.SaveViewState();
    }

    protected override void LoadViewState(object savedState)
    {
        base.LoadViewState(savedState);
        if (ViewState["Mode"] is Modes)
        {
            currentMode = (Modes)ViewState["Mode"];
        }
    }

    protected void lnkSelectUser_Click(object sender, EventArgs e)
    {
        pnlDetails.Visible = false;
        gvUsers.Visible = true;
        SetMode(Modes.SelectUser);
    }

    protected void tabUser_TabClick(object sender, Telerik.Web.UI.RadTabStripEventArgs e)
    {
        tabUser.TabClick += new Telerik.Web.UI.RadTabStripEventHandler(tabUser_TabClick);
        if (pvUserDetails.Selected)
        {
            SetMode(Modes.UserDetailsView);
        }
        else if (pvUserRoles.Selected)
        {
            SetMode(Modes.UserRolesDisplay);
        }
    }

    protected void lnkEdit_Click(object sender, EventArgs e)
    {
        if (CurrentMode == Modes.UserDetailsView)
        {
            SetMode(Modes.UserDetailsEdit);
        }
    }

    protected void lnkSave_Click(object sender, EventArgs e)
    {
        if (CurrentMode == Modes.UserDetailsEdit)
        {
            if (IsValid)
            {
                //save user
                dvUserDetails.UpdateItem(true);
                SetMode(Modes.UserDetailsView);
            }
        }
    }

    protected void lnkCancel_Click(object sender, EventArgs e)
    {
        if (CurrentMode == Modes.UserDetailsEdit)
        {
            SetMode(Modes.UserDetailsView);
        }
        else if (CurrentMode == Modes.UserRolesAdd)
        {
            SetMode(Modes.UserRolesDisplay);
        }
    }
    protected void lnkAddRole_Click(object sender, EventArgs e)
    {
        SetMode(Modes.UserRolesAdd);
    }
    protected void dsRoles_SelectData(object sender, Acms.DataControls.SelectEventArgs<Acms.Bll.Role> e)
    {
        e.SetData(Acms.Bll.Role.GetAllBySiteId(AcmsContext.SiteId));
    }
}
